Street Coder by Sedat Kapanoglu

Street Coder by Sedat Kapanoglu

Author:Sedat Kapanoglu [Sedat Kapanoglu]
Language: eng
Format: epub, mobi
Publisher: Manning Publications
Published: 2022-01-17T16:00:00+00:00


int

32-bit signed

-2147483648..2147483647

Duh

uint

32-bit unsigned

0..4294967295

No

long

64-bit signed

-9223372036854775808..9223372036854775807

No

ulong

64-bit unsigned

0..18446744073709551615

No

short

16-bit signed

-32768..32767

Yes

ushort

16-bit unsigned

0..65535

Yes

sbyte

8-bit signed

-128..127

Yes

byte

8-bit unsigned

0..255

Yes

When you use an unsigned type, trying to pass a negative constant value to your function will cause a compiler error. Passing a variable with a negative value is possible only with explicit type casting, which makes you think about whether the value you have is really suitable for that function at the call site. It’s not the function’s responsibility to validate for negative arguments anymore. Assume that a function needs to return trending tags in your microblogging website up to only a specified number of tags. It receives a number of items to retrieve rows of posts, as in listing 4.10.

Also in listing 4.10, a GetTrendingTags function returns items by taking the number of items into account. Notice that the input value is a byte instead of int because we don’t have any use case more than 255 items in the trending tag list. That actually immediately eliminates the cases where an input value can be negative or too large. We don’t even need to validate the input anymore. This results in one fewer test case and a much better range of input values, which reduces the area for bugs immediately.

Listing 4.10 Receiving posts only belonging to a certain page

using System; using System.Collections.Generic; using System.Linq; namespace Posts { public class Tag { public Guid Id { get; set; } public string Title { get; set; } } public class PostService { public const int MaxPageSize = 100; private readonly IPostRepository db; public PostService(IPostRepository db) { this.db = db; } public IList<Tag> GetTrendingTags(byte numberOfItems) { ❶ return db.GetTrendingTagTable() .Take(numberOfItems) ❷ .ToList(); } } }

❶ We chose byte instead of int.

❷ A byte or a ushort can be passed as safely as int too.

Two things are happening here. First, we chose a smaller data type for our use case. We don’t intend to support billions of rows in a trending tag box. We don’t even know what that would look like. We have narrowed down our input space. Second, we chose byte, an unsigned type, which cannot be negative. That way, we avoided a possible test case and a potential problem that might cause an exception. LINQ’s Take function doesn’t throw an exception with a List, but it can when it gets translated to a query for a database like Microsoft SQL Server. By changing the type, we avoid those cases, and we don’t need to write tests for them.

Note that .NET uses int as the de facto standard type for many operations like indexing and counting. Opting for a different type might require you to cast and convert values into ints if you happen to interact with standard .NET components. You need to make sure that you’re not digging yourself into a hole by being pedantic. Your quality of life and the enjoyment you get from writing code are more important than a certain one-off case you’re trying to avoid. For example, if you need more than 255 items in the future, you’ll have to replace all references to bytes with shorts or ints, which can be time consuming.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.